package com.xxh.market.dao.impl;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.xxh.market.dao.UserDao;
import com.xxh.market.entity.User;
import com.xxh.market.utils.jdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl implements UserDao
{
    Connection conn=null;
    PreparedStatement psmt=null;
    ResultSet rs=null;
    User currUser=null;
    @Override
    public User login(String username, String password) {
        try {
            conn=jdbcUtils.getConnection();
            String sql="select * from user where username=? and password=?";
            psmt=conn.prepareStatement(sql);
            psmt.setString(1, username);
            psmt.setString(2, password);
            rs=psmt.executeQuery();
            if(rs.next()) {
                int id=rs.getInt(1);
                String user=rs.getString(2);
                String pwd=rs.getString(3);
                currUser=new User();
                currUser.setId(id);
                currUser.setUsername(user);
                currUser.setPassword(pwd);
            }
            return currUser;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            jdbcUtils.close(rs, psmt, conn);
        }
        return null;
    }
}
